package com.datastructures2.字符串.子字符串查找;

/**
 * @author MaoLin Wang
 * @date 2020/2/2712:21
 */
public class ViolenceFind {
    public static int search(String pat,String text){
        int j , M = pat.length();
        int i , N = text.length();

        for (i = 0, j = 0; i < N && j < M; i++) {
            if (text.charAt(i) == pat.charAt(j)){
                //匹配，指向模式的指针加1
                j++ ;
            }else {
                //不匹配，i回退到此次匹配的开头，j重新指向模式的开头
                i -= j;
                j = 0;
            }
        }
        if (j==M){
            return i - M;
        }else {
            return  N;
        }
    }

    public static void main(String[] args) {
        String text="abcdabce";
        int bce = search("bce", text);
        System.out.println(bce);
    }
}
